import {IChatUser, ICustomer} from "@/types/store/customer";
import {ICustomerInfo} from "@/api/types/customer";
import {getCustomerUnreadCount, queryMessageList} from "@/api/customer";

const state: ICustomer = {
  loginId: 0,
  messageUser: [],
  user: {},
  activeUser: null, // 當前聊天用戶
  // 客服朋友列表
  friendList: [],
  pendingUser: 0,
  pendingMessage: 0,
  chatList: [],
  chatUpdateId: 0,
  chatTime: 0,
}

const mutations = {
  // 设置当前聊天的客戶
  async setActiveUser(state: ICustomer, user: IChatUser) {
    state.activeUser = user
    // 1. 查询客户是待处理消息记录数
    const count = await getCustomerUnreadCount(user.username, state.user.openId!, state.loginId)
    // 2. 查询聊天信息
    state.chatList = await queryMessageList({
      username: user.username,
      unread: count!
    })
    // 3.更新消息置底部
    setTimeout(() => {
      state.chatUpdateId++
    }, 200)
  },
  setCustomerUser(state: ICustomer, user: ICustomerInfo) {
    state.user = user // 设置客服用户
  }
}

const customerState = {
  namespaced: true,
  state,
  mutations,
}

export default customerState